<template>
  <ele-modal
    :width="750"
    :visible="visible"
    :confirm-loading="loading"
    :title="actionStatus == 'edit' ? '修改' : '新建'"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <a-form
      :label-col="{ md: { span: 7 }, sm: { span: 4 }, xs: { span: 24 } }"
      :wrapper-col="{ md: { span: 17 }, sm: { span: 20 }, xs: { span: 24 } }"
    >
      <a-row :gutter="24">
        <a-col :md="20" :sm="24" :xs="24">
          <a-form-item label="扣减原因" v-bind="validateInfos.deduct_cause">
            <div style="display: flex">
              <a-select
                optionFilterProp="label"
                :options="lossCause"
                allow-clear
                v-model:value="form.deduct_cause"
                placeholder="请选择"
                @change="lossCauseChange"
                autocomplete="off"
              />
              <img
                src="@/assets/set.png"
                style="
                  width: 30px;
                  height: 30px;
                  margin-left: 20px;
                  cursor: pointer;
                "
                @click="showList()"
              />
            </div>
          </a-form-item>
          <a-form-item label="备注"
            ><a-textarea
              placeholder="请输入备注"
              allow-clear
              v-model:value="form.note"
          /></a-form-item>
        </a-col>
      </a-row>
    </a-form>
    <reason-list
      v-if="reasonListShow"
      v-model:visible="reasonListShow"
      @getReason="getReason"
    />
  </ele-modal>
</template>

<script setup>
  import { ref, reactive, onMounted } from 'vue';
  import { Form } from 'ant-design-vue';
  import { emailReg, phoneReg, identityReg } from 'ele-admin-pro';
  import reasonList from './reasonList.vue';
  import {
    get_warehouse_list,
    get_store_list,
    get_store_group_list,
    get_user_list,
    get_receipts_cause
  } from '@/api/jewelry/receipts_purchase';
  import { get_detail } from '@/api/member/receipts_points_deduct';
  // 表格列配置
  const useForm = Form.useForm;
  const props = defineProps({
    // 弹窗是否打开
    visible: Boolean,
    // 编辑edit/新建add
    actionStatus: String,
    // 编辑数据id
    editData: Object
  });
  const reasonListShow = ref(false);
  const emit = defineEmits(['done', 'update:visible']);
  const action = () => {
    reasonListShow.value = true;
  };
  // 提交状态
  const loading = ref(false);
  const lossCause = ref([]);
  onMounted(async () => {
    //赠送原因
    await getReason();
    if (props.editData.code) {
      form.code = props.editData.code;
      await get_detail({ code: props.editData.code }).then((res) => {
        Object.keys(form).forEach((key) => {
          form[key] = res.receipts_detail ? res.receipts_detail[key] : '';
        });
      });
    }
  });
  const getReason = () => {
    get_receipts_cause({ page: 1, page_size: 9999 }).then((res) => {
      lossCause.value = res.list.map((item) => {
        return {
          value: String(item.id),
          label: item.content
        };
      });
    });
  };

  const lossCauseChange = (value) => {
    form.deduct_cause = value;
  };
  const showList = () => {
    reasonListShow.value = true;
  };
  // 表单数据
  const form = reactive({
    code: '',
    deduct_cause: null,
    note: ''
  });
  // 表单验证规则
  const rules = reactive({
    deduct_cause: [
      {
        required: true,
        message: '请选择扣减原因',
        trigger: 'change'
      }
    ]
  });

  const { validate, validateInfos } = useForm(form, rules);
  /* 保存编辑 */
  const save = () => {
    validate()
      .then(() => {
        emit('done', form);
      })
      .catch(() => {});
  };
  /* 更新visible */
  const updateVisible = (value) => {
    emit('update:visible', value);
  };
</script>
<style scoped></style>
